package com.yksj.healthtalk.ui.interestwall;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.entity.GroupInfoEntity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonHttpResponseHandler;
import com.yksj.healthtalk.net.http.RequestParams;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.friend.FriendSearchMainActivity;
import com.yksj.healthtalk.ui.home.MyFriendSearchActivity;
import com.yksj.healthtalk.ui.home.MySalonSearchActivity;
import com.yksj.healthtalk.ui.salon.SalonSearchMainActivity;
import com.yksj.healthtalk.utils.ViewFinder;
/**
 * 兴趣墙转发主页
 * 转发 好友  话题
 * @author jack_tang
 *
 */
public class InterestwallForwardActivity extends BaseFragmentActivity implements OnClickListener {
	private int tag;//1 我的朋友  2 我的话题
	private ViewFinder mFinder;
	private JSONArray jsonArray;
	private Intent intent;
	private final int SEACH_CODE = 1000;
	private final int SEACH_SALON = 2000;
	private GroupInfoEntity mGroupInfoEntity;
	private String flagPlacing;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.forward_main_layout);
		initView();
	}

	private void initView() {
		initTitle();
		titleLeftBtn.setOnClickListener(this);
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setText("搜索");
		titleRightBtn2.setOnClickListener(this);
		
		tag = getIntent().getIntExtra("tag",2);
			if(tag==1){
				titleTextV.setText("我的朋友");
			}else if(tag==2){
				titleTextV.setText("我的话题");
			}
			
		mFinder = new ViewFinder(this);
		mFinder.onClick(InterestwallForwardActivity.this, new int[]{R.id.layout1,R.id.layout2});
		
		
		flagPlacing = "-100000";
		mGroupInfoEntity = new GroupInfoEntity();
		mGroupInfoEntity.setFlag("");
		mGroupInfoEntity.setFlagPlacing(flagPlacing);
		mGroupInfoEntity.setPagesize(1);
		mGroupInfoEntity.setType(1);
		mGroupInfoEntity.setCreateCustomerID(SmartFoxClient.getLoginUserId());
		
		
		initData();
	}

	/**
	 * 请求数量
	 */
	private void initData() {
		RequestParams params=new RequestParams();
		params.put("CUSTOMERID", SmartFoxClient.getLoginUserId());
		if(tag==2){
			params.put("OPTION", "TALKCOUNT");	
		}else if(tag==1){
			params.put("OPTION", "ATTENTIONCOUNT");
		}
		
		HttpRestClient.doHttpInterestForwardNum(params,new JsonHttpResponseHandler(this){

			@Override
			public void onSuccess(int statusCode, JSONObject response) {
				super.onSuccess(statusCode, response);
						jsonArray = response.optJSONArray("INFO");
						JSONObject jsonObject = jsonArray.optJSONObject(0);
						JSONObject jsonObject1 = jsonArray.optJSONObject(1);
						mFinder.setText(R.id.text1, jsonObject.optString("VALUE")+"("+jsonObject.optString("COUNT")+")");
						mFinder.setText(R.id.text2,  jsonObject1.optString("VALUE")+"("+jsonObject1.optString("COUNT")+")");
						if(tag==2){
							mFinder.setDrawable(R.id.image1, R.drawable.huati_woguanzhude);
							mFinder.setDrawable(R.id.image2, R.drawable.huati_wochuangjiande);
						}else{
							mFinder.setDrawable(R.id.image1, R.drawable.bingyou_guanzhuwode);
							mFinder.setDrawable(R.id.image2, R.drawable.bingyou_woguanzhude);
						}
				}
		});
	}

	
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent data) {
		super.onActivityResult(arg0, arg1, data);
		if(data==null) return;
		switch (arg0) {
		case SEACH_CODE:
			if (data != null && data.hasExtra("mCustomerInfoEntity")) {
				CustomerInfoEntity mCustomerInfoEntity = data.getParcelableExtra("mCustomerInfoEntity");
				Intent intent = new Intent(this,MyFriendSearchActivity.class);
				intent.putExtra("type", true);
				intent.putExtra("picID", getIntent().getStringExtra("picID"));
				intent.putExtra("mCustomerInfoEntity", mCustomerInfoEntity);
				startActivity(intent);
			}
			break;
		case SEACH_SALON:
			flagPlacing = "-100000";
			if (data.hasExtra("positions")) {
				ArrayList<String> list = (ArrayList<String>) data.getExtras().get("positions");
				mGroupInfoEntity = (GroupInfoEntity) data.getExtras().get("mGroupInfoEntity");
				String dataClickId = (String) data.getExtras().get("dataClickId");
				// 不是从搜索首页全部搜索
				if (!dataClickId.equals("0")) {
					mGroupInfoEntity.setFlag("");
					mGroupInfoEntity.setFlagPlacing(flagPlacing);
					mGroupInfoEntity.setType(1);
					mGroupInfoEntity.setUpperId(dataClickId);
					StringBuilder builder = new StringBuilder();
					for (int i = 0; i < list.size(); i++) {
						builder.append(Integer.valueOf(list.get(i)));
						builder.append(",");
					}
					builder.deleteCharAt(builder.toString().length() - 1);
					mGroupInfoEntity.setInfoId(builder.toString());
				} else {
					mGroupInfoEntity.setFlag("");
					mGroupInfoEntity.setFlagPlacing(flagPlacing);
					mGroupInfoEntity.setType(1);
					mGroupInfoEntity.setUpperId(dataClickId);
					mGroupInfoEntity.setInfoId("0");
				}
			}
			Intent intent = new Intent(getApplicationContext(), MySalonSearchActivity.class);
			intent.putExtra("mGroupInfoEntity", mGroupInfoEntity);
			intent.putExtra("picID", getIntent().getStringExtra("picID"));
			startActivity(intent);
			break;
		}
	}
	
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.title_right2:
			if(tag==1){//我的朋友
				Intent intent = new Intent();
				intent.putExtra("search_type", 1);
				intent.putExtra("adapter_type", 1);
				intent.setClass(this, FriendSearchMainActivity.class);
				intent.putExtra("picID", getIntent().getStringExtra("picID"));
				startActivityForResult(intent, SEACH_CODE);
			}else{//我的沙龙
				Intent 	intent = new Intent();
				intent.setClass(getApplicationContext(), SalonSearchMainActivity.class);
				intent.putExtra("picID", getIntent().getStringExtra("picID"));
				intent.putExtra("mGroupInfoEntity", mGroupInfoEntity);
				startActivityForResult(intent, SEACH_SALON);
			}
			break;
		case R.id.layout1://我关注的
//			MYATTENTION 我关注的人
//			MYATTENTIONGROUP 我关注的群
			intent = new Intent(this, ForwardListActivity.class);
			if(tag==2){//ATTENTIONINFO
				intent.putExtra("OPTION","TALKINFO");	
				intent.putExtra("flag","MYATTENTIONGROUP");	
			}else if(tag==1){
				intent.putExtra("OPTION", "ATTENTIONINFO");
				intent.putExtra("flag", "MYATTENTION");
			}
			intent.putExtra("tag", tag);
			intent.putExtra("picID", getIntent().getStringExtra("picID"));
			intent.putExtra("TYPE", jsonArray.optJSONObject(0).optString("KEY")) ;
			intent.putExtra("title",jsonArray.optJSONObject(0).optString("VALUE"));
			startActivity(intent);
			break;
		case R.id.layout2://关注我的
//			ATTENTIONME 关注我的人
//			MYCREATE 我创建的群
			intent=new Intent(this, ForwardListActivity.class);
			if(tag==2){
				intent.putExtra("OPTION", "TALKINFO");	
				intent.putExtra("flag", "MYCREATE");	
			}else if(tag==1){
				intent.putExtra("OPTION", "ATTENTIONINFO");
				intent.putExtra("flag", "ATTENTIONME");
			}
			intent.putExtra("picID", getIntent().getStringExtra("picID"));
			intent.putExtra("tag", tag);
			intent.putExtra("TYPE", jsonArray.optJSONObject(1).optString("KEY"));
			intent.putExtra("title",jsonArray.optJSONObject(1).optString("VALUE"));
			startActivity(intent);
			break;
		}
	}

}
